package com.gdit.framework.common.vo;

import java.io.Serializable;
import java.util.List;

import com.gdit.framework.common.util.Constants;
import com.gdit.framework.common.util.StringUtil;


public class PageVO<T>  implements Serializable{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	private int pageSize;// 每页记录数

	private int pageNo ;// 当前页

	private int startIndex ;

	private int endIndex;

	private List<T> resultList;// 结果集

	private int rowCount; // 总记录数

	private int pageNum;// 总页数
	
	private String currentPage;//跳转到的页码
	
	private String pageRecords;//每页记录数
	
	public PageVO(){
		
		this.pageNo = Constants.PAGE_NO;
		this.pageSize = Constants.PAGE_SIZE;
	}
	
	public PageVO(int rowCount){
		
		this.rowCount = rowCount;
		this.pageNo = Constants.PAGE_NO;
		this.pageSize = Constants.PAGE_SIZE;
	}
	
	public PageVO(int rowCount, int pageNo, int pageSize) {
		if (pageSize <= 0) {
			this.pageSize = Constants.PAGE_NO;
		} else {
			this.pageSize = pageSize;
		}
		this.setRowCount(rowCount);
		
		if (pageNo <= 0) {
			this.pageNo = Constants.PAGE_NO;
		} else if (pageNo > this.pageNum) {
			this.pageNo = this.pageNum;
		} else {
			this.pageNo = pageNo;
		}
		
		this.setStartIndex(0);
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getStartIndex() {
		if(this.pageNo<1){
			this.pageNo=1;
		}
		return this.pageSize * (this.pageNo - 1);
	}

	public void setStartIndex(int startIndex) {
		this.startIndex = this.pageSize * (this.pageNo - 1);
	}

	public int getEndIndex() {
		return this.startIndex+this.pageSize;
	}

	public void setEndIndex(int endIndex) {
		this.endIndex = endIndex;
	}

	public List<T> getResultList() {
		return resultList;
	}

	public void setResultList(List<T> resultList) {
		this.resultList = resultList;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		if (rowCount <= 0) {
			this.rowCount = 0;
		} else {
			this.rowCount = rowCount;
		}
		this.pageNum = (this.rowCount - 1) / this.pageSize + 1;
	}

	public int getPageNum() {
		return pageNum;
	}

	public void setPageNum(int pageNum) {
		this.pageNum = pageNum;
	}
	
	/***
	 * 页面传进来的值
	 */
	public String getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(String currentPage) {
		if (StringUtil.isEmpty(currentPage)) {
			this.pageNo = Constants.PAGE_NO;
		}
		else {
			try {
				this.pageNo = Integer.parseInt(currentPage.toString());
			}
			catch (NumberFormatException e) {
				this.pageNo= Constants.PAGE_NO;
			}
		}
		
	}

	public String getPageRecords() {
		return pageRecords;
	}

	public void setPageRecords(String pageRecords) {
		if (StringUtil.isEmpty(pageRecords)) {
			this.pageSize = Constants.PAGE_SIZE;
		}
		else {
			try {
				this.pageSize = Integer.parseInt(pageRecords.toString());
			}
			catch (NumberFormatException e) {
				this.pageSize= Constants.PAGE_SIZE;
			}
		}
	}
	
}
